package streaming;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;


//单词计数之滑动窗口计算
public class WordCount {
    public static void main(String[] args) throws Exception{
        //获取需要的端口号
        int port;
        try{
            //ParameterTool 是Flink 提供的读取程序启动参数、配置文件、环境变量以及Flink自身配置参数等配置的一个工具类
            ParameterTool parameterTool = ParameterTool.fromArgs(args);
            //程序启动时，传入参数--port
            //也可以使用ParameterTool从properties获取配置
            port = parameterTool.getInt("port");
        }catch (Exception e){
            System.err.println("No port set. use default port 9000-Java");
            port = 9000;
        }
        //获取flink的运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        String hostname = "hadoop100";
        String delimiter = "\n";

        //连接Socket获取输入的数据
        DataStreamSource<String> text = env.socketTextStream(hostname, port, delimiter);
        //解析数据(把数据打平)，分组，窗口计算，并且聚合求sum
        //flatMap用于输入一个元素，但要输出多个元素时，比如本例中的分割
        DataStream<WordWithCount> windowCounts = text.flatMap(new FlatMapFunction<String, WordWithCount>() {
            public void flatMap(String value, Collector<WordWithCount> out) throws Exception {
                String[] splits = value.split("\\s");  //'\\s'代表空格，回车，换行等符号
                for (String word : splits){
                    out.collect(new WordWithCount(word, 1L));
                }
            }
        }).keyBy("word")
                .timeWindow(Time.seconds(2), Time.seconds(1))  //指定时间窗口大小为2s，指定时间间隔为1s
                .sum("count");
        //将数据打印到控制台并且设置并行度
        windowCounts.print().setParallelism(1);
        //这一行代码一定要实现，否则程序不执行
        env.execute("Socket window count");
    }
    public static class WordWithCount{
        public String word;
        public long count;
        public WordWithCount(){}
        public WordWithCount(String word, long count){
            this.word = word;
            this.count = count;
        }

        @Override
        public String toString(){
            return "WordWithount{" +
                    "word = '"+word+'\''+
                    ", count = "+count+
                    '}';
        }
    }
}
